package com.shambles.manage2.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.CattleService;
import com.shambles.manage2.tkmapper.entity.AbattoirBooth;
import com.shambles.manage2.tkmapper.entity.Cattle;
import com.shambles.manage2.tkmapper.entity.MessageRequestVM;
import com.shambles.manage2.tkmapper.entity.SysLog;
import com.shambles.manage2.tkmapper.mapper.CattleMapper;
import com.shambles.manage2.tkmapper.mapper.SysLogMapper;
import com.shambles.manage2.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class CattleServiceImpl implements CattleService {
    @Autowired
    private CattleMapper cattleMapper;
    @Autowired
    private SysLogMapper sysLogMapper;
    @Override
    public Map<String, Cattle> getCattleMap() {
        List<Cattle> cattles = cattleMapper.selectAll();
        return cattles.stream().collect(Collectors.toMap(Cattle::getGuid,Cattle->Cattle));
    }

    /**
     * 添加种类信息
     * @param cattle
     */
    @Override
    public void insertCattle(Cattle cattle) {
        Cattle cattle1=new Cattle();
        cattle1.setGuid(IdUtil.simpleUUID());
        cattle1.setPrice(cattle.getPrice());
        cattle1.setType(cattle.getType());
        cattle1.setSlaughterTime(cattle.getSlaughterTime());

        this.cattleMapper.insertSelective(cattle1);
    }

    /**
     * 修改种类信息
     * @param cattle
     */
    @Override
    public void updateCattle(Cattle cattle) {
        String Guid = cattle.getGuid();
        Weekend<Cattle> cattleWeekend = new Weekend<>(Cattle.class);
        cattleWeekend.weekendCriteria().andEqualTo(Cattle::getGuid, Guid);
        /**
         * 添加日志信息
         */
        Cattle cattlePrice= this.cattleMapper.queryPrice(cattle.getGuid());
        float price= cattlePrice.getPrice();
        SysLog sysLog=new SysLog();
        sysLog.setGuid(IdUtil.simpleUUID());
        sysLog.setOperationPeople(CommonUtils.getCurrentUserName());
        sysLog.setFunctionName("种类");
        sysLog.setContent("修改前金额"+price+"修改后金额"+cattle.getPrice());
        sysLog.setOperationTime(System.currentTimeMillis());
        this.sysLogMapper.insertSelective(sysLog);

        this.cattleMapper.updateByExampleSelective(cattle, cattleWeekend);


    }

    /**
     * 分页查询种类信息
     * @param messageRequestVM
     * @return
     */
    @Override
    public PageInfo<Cattle> queryCattle(MessageRequestVM messageRequestVM) {
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                cattleMapper.queryCattle()
        );
    }
}
